<template>
	<view class="container">
		<view class="blue_bg" />
		<view class="coupon_tab">
			<!-- 判断是否有符合条件的优惠券 -->
			<view v-if="filteredCoupons.length > 0">
				<!-- 循环显示符合条件的优惠券 -->
				<view v-for="(items, index) in filteredCoupons" :key="index" :class="{
						'coupon_item': true,
						'coupon_over': active === 0, 
						'coupon_item coupon_overdue': active === -1
					}">
					<view class="item_up">
						<view class="left">
							{{ items.name }}
							<text>满{{ items.full }}元可用</text>
						</view>
						<view class="right">
							￥<text :class="{ 'del': active === -1 }">{{ items.reduce }}</text>
						</view>
					</view>
					<view class="item_down">
						<view class="left">
							有效期至: {{ items.useTime }} — {{ items.useendTime }}
						</view>
						<view class="right" @click="add(items)">
							去使用 >
						</view>
					</view>
				</view>
			</view>
			<!-- 没有符合条件的优惠券时 -->
			<view v-else class="no_coupon">
				没有可用优惠券
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: 0,
				item: {}, // 当前选中商品数据
				coupons: [], // 优惠券列表
				active: 1, // 用于控制优惠券的状态
			};
		},
		onLoad(e) {
			// 获取用户 ID 和传递的商品数据
			this.userId = uni.getStorageSync("user_id");
			this.item = JSON.parse(decodeURIComponent(e.item));
			console.log(this.item);
			this.fetchCoupons();
		},
		computed: {
			// 筛选出符合条件的优惠券（full 小于 selectedService.newPrice）
			filteredCoupons() {
				return this.coupons.filter((coupon) => coupon.full < this.item.selectedService.newPrice);
			},
		},
		methods: {
			// 跳转页面并携带选中的优惠券数据
			add(selectedCoupon) {
				const item = {
					...this.item,
					...selectedCoupon,
				};
				uni.navigateTo({
					url: `/pages/lzx/confirmOrder?item=${encodeURIComponent(JSON.stringify(item))}`,
				});
			},
			// 获取优惠券列表
			fetchCoupons() {
				this.$http({
					url: "/api/shanghuliebiao2/coupon/allList",
					method: "GET",
					success: (res) => {
						console.log(res);
						if (res.data.code === 200) {
							this.coupons = res.data.data || [];
						}
					},
					fail: (err) => {
						console.error("Failed to fetch coupons:", err);
					},
				});
			},
		},
	};
</script>

<style>
	@import '@/static/images/ryx/all.css';

	.coupon_til {
		width: 100vw;
		box-sizing: border-box;
		padding: 20rpx 40rpx;
		font-size: 40rpx;
		color: #fff;
		background-color: #2679f5;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
	}

	.active {
		border-bottom: #fff solid 2px;
	}

	.coupon_tab {
		width: 90vw;
		padding: 20rpx;
		margin-top: 30rpx;
		border-radius: 20rpx;
		background-color: #fff;
	}

	.coupon_item {
		height: 19vh;
		box-sizing: border-box;
		padding: 0 26rpx;
		margin: 20rpx;
		background-image: url('@/static/images/ryx/asset/bg_@2x.png');
		background-repeat: no-repeat;
		background-size: 100%;
		display: flex;
		flex-direction: column;
		color: #fff;
	}

	.coupon_over {
		background-image: none;
		background-color: #ffbb99;
	}

	.coupon_overdue {
		background-image: none;
		background-color: #bfbfbf;
	}

	.coupon_item .item_up {
		height: 80%;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
	}

	.coupon_item .item_up .left {
		display: flex;
		flex-flow: column;
	}

	.coupon_item .item_up .left text {
		font-size: 40rpx;
		font-weight: bold;
	}

	.coupon_item .item_up .right text {
		font-size: 80rpx;
		font-weight: bold;
	}

	.coupon_item .item_down {
		height: 40%;
		border-top: #fff dashed 1rpx;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
	}

	.no_coupon {
		text-align: center;
		font-size: 36rpx;
		color: #888;
		padding: 40rpx 0;
	}
</style>